<?php
namespace Core\Common\Model;

/**
 * 分表能力
 * Class DynamicBuilder
 * @package App\Model\Base
 */
class DynamicBuilder extends CachedBuilder
{
    /**
     * 确定分表并获取Query
     * @param $name
     * @return $this
     */
    public function dynamic($name)
    {
        /* @var \Core\Common\Model\Base\AbstractDynamicModel $model */
        $model = $this->getModel();

        if (is_array($name)) {
            $table = $model::getTableByAttr($name);
        } else {
            $table = $name;
        }

        $model->setTable($table);
        $this->setModel($model);

        return $this;
    }

    /**
     * @param array $columns
     * @return CachedBuilder[]|\Illuminate\Database\Eloquent\Collection|mixed
     */
    public function get($columns = ['*'])
    {
        if (!$this->model->getTable()) {
            throw new \RuntimeException('Not switch table before query.');
        }
        return parent::get($columns);
    }
}